/*
 * LoginFrame.java
 *
 * Created on __DATE__, __TIME__
 */

package com.beilove.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;

import org.apache.log4j.Logger;

import com.beilove.action.LoginAction;
import com.beilove.event.LoginActionImpl;

/**
 * 
 * @author __USER__
 */
public class LoginFrame extends javax.swing.JFrame {

	private static Logger log = Logger.getLogger(LoginFrame.class);

	/** Creates new form LoginFrame */
	public LoginFrame() {
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		userName = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		passwd = new javax.swing.JPasswordField();
		btnSubmit = new javax.swing.JButton();
		btnCancel = new javax.swing.JButton();
		msg = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("\u4e2a\u4eba\u4fe1\u606f\u7ba1\u7406\u7cfb\u7edf");

		jLabel1.setText("\u7528\u6237\u540d\uff1a");

		jLabel2.setText("\u5bc6\u7801\uff1a");

		btnSubmit.setText("\u767b\u5f55");

		btnCancel.setText("\u9000\u51fa");

		// Event Handler
		btnCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

		btnSubmit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String txtUserName = userName.getText();
				String txtPwd = new String(passwd.getPassword());
				log.debug("userName:" + txtUserName + ";passwd:" + txtPwd);
				boolean logFlag = new LoginAction().login(txtUserName,txtPwd);
				if(logFlag){
					LoginFrame.this.dispose();
					JFrame main = new MainJFrame();
					main.setExtendedState(JFrame.MAXIMIZED_BOTH);
					main.setVisible(true);
				}
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addGap(80, 80,
																		80)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING,
																				false)
																				.addComponent(
																						jLabel2,
																						javax.swing.GroupLayout.Alignment.TRAILING,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						Short.MAX_VALUE)
																				.addComponent(
																						jLabel1,
																						javax.swing.GroupLayout.Alignment.TRAILING,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						Short.MAX_VALUE))
																.addGap(37, 37,
																		37)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING,
																				false)
																				.addComponent(
																						passwd,
																						0,
																						0,
																						Short.MAX_VALUE)
																				.addComponent(
																						userName,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						148,
																						Short.MAX_VALUE)
																				.addComponent(
																						msg,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						Short.MAX_VALUE)))
												.addGroup(
														layout.createSequentialGroup()
																.addGap(122,
																		122,
																		122)
																.addComponent(
																		btnSubmit)
																.addGap(55, 55,
																		55)
																.addComponent(
																		btnCancel)))
								.addContainerGap(87, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(95, 95, 95)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														jLabel1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														25,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														userName,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(34, 34, 34)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(jLabel2)
												.addComponent(
														passwd,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										19, Short.MAX_VALUE)
								.addComponent(msg,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										21,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(btnCancel)
												.addComponent(btnSubmit))
								.addGap(46, 46, 46)));

		pack();
	}// </editor-fold>
		// GEN-END:initComponents

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				JFrame loginFrame = new LoginFrame();
				loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				loginFrame.setVisible(true);
			}
		});
	}

	// GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton btnCancel;
	private javax.swing.JButton btnSubmit;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel msg;
	private javax.swing.JPasswordField passwd;
	private javax.swing.JTextField userName;
	// End of variables declaration//GEN-END:variables

}